﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MyWebPagesStarterKit;
using MyWebPagesStarterKit.Utils;

public partial class EasyControls_BlogTeaser : System.Web.UI.UserControl
{
    private string fromSectionID = "b598915a-28e0-4bc5-9c91-2bc63fe466e2"; //Set section of the blogs sectionid before use.
    private string page = "/blog.aspx"; //The page you want to get blog entries from.
    private int HowManyToShow = 5; //How many blog posts to show.


    private RBlog _section;
    private string glbComment, glbComments, glbAuthor;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _section = (RBlog)SectionLoader.GetInstance().LoadSection(fromSectionID);
            if(_section!=null)
            {
                glbComment = Resources.StringsRes.ctl_Blog_Comment;
                glbComments = Resources.StringsRes.ctl_Blog_Comments;
                glbAuthor = Resources.StringsRes.ctl_Blog_CommentsAuthor;
                LoadData();
            }
        }
    }


    private void LoadData()
    {
        lstBlogEntries.DataSource = new FilterRows(_section.GetEntriesReadonlyByDate(), HowManyToShow);
        lstBlogEntries.DataBind();
    }

    protected string Counted(object id)
    {
        DataRow row = _section.GetEditEntryReadOnly(Convert.ToString(id));
        if (!DBNull.Value.Equals(row["Comments"]))
        {
            List<RBlog.Comment> comments = ((List<RBlog.Comment>)(row["Comments"]));

            string text = string.Empty;

            if (comments.Count > 1)
                text = string.Format(" ({0}) {1}", comments.Count.ToString(), glbComments);
            else if (comments.Count == 1)
                text = string.Format(" (1) {0}", glbComment);
            else
                text = string.Format(" (0) ", glbComments);

            return text;
        }
        return string.Format(" (0) {0}", glbComments);
    }

    protected string GetTags(object id)
    {
        string t = string.Empty;
        try
        {
            DataRow row = _section.GetEditEntryReadOnly(id.ToString());

            if (!DBNull.Value.Equals(row["Tags"]))
            {
                List<RBlog.Tags> tags = ((List<RBlog.Tags>)(row["Tags"]));

                if (tags != null)
                {
                    foreach (RBlog.Tags item in tags)
                        t += string.Format("<a href=\"{0}?tag={1}\">{1}</a> ", page, item.Name);
                }
            }
        }
        catch { }
        return t;
    }

    /// <summary>
    /// Adds date and url
    /// </summary>
    /// <param name="link">The link to perform rewrite prefix to.</param>
    /// <returns>A browserfriendly url</returns>
    protected string GetPrefix(string link)
    {
        return string.Format("{0}/{1}/{2}", "blog", DateTime.Now.ToShortDateString(), link).ToLower();
    }
}
